﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using System.Data;

namespace HZ.DrugTrade.Web.Controls
{
    public partial class Menu2 : System.Web.UI.UserControl
    {
        IBLL_OperatingRule operateRuleCtrl = Factory.BLL_OperatingRule();

        //
        DataTable _allMenuTable;
        DataTable _menuTable;
        DataTable _mainMenuTable;

        /// <summary>
        /// 装载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //开发模式develop 生产模式 product
            string model = "product";
            string userId = CurrentAccount.GetUser().UserID;
            string roleId = CurrentAccount.GetUser().UserRole.RoleID;

            IList<HZ.DrugTrade.Model.View.UserMenuRule> menuList;
            IList<HZ.DrugTrade.Model.View.UserMenuRule> allMenuList;

            //获取所有菜单集合
            allMenuList = operateRuleCtrl.GetAllUserMenuRule( roleId );

            if (model == "develop")
            {
                //开发模式下获取所有菜单
                menuList = operateRuleCtrl.GetAllUserMenuRule( roleId );

            }
            else
            {
                //根据用户权限获取相应菜单
                menuList = operateRuleCtrl.GetUserMenuRule(userId);
            }
            
            //将泛型集合转换成DataTable
            _allMenuTable = HZ.Data.ModelConvert.GetDataTableFromModel(allMenuList);
            _menuTable = HZ.Data.ModelConvert.GetDataTableFromModel(menuList);
            _allMenuTable.PrimaryKey = new DataColumn[] { _allMenuTable.Columns["MenuID"] };

            //取得所有主菜单 目前只取的主菜单的ID
            _mainMenuTable = _menuTable.DefaultView.ToTable(true,"ParentMenuID");

            //增加列
            _mainMenuTable.Columns.Add(new DataColumn("MenuName", typeof(System.String)));
            _mainMenuTable.Columns.Add(new DataColumn("Sort", typeof(System.Int32)));

            //填充主菜单名称、排序
            foreach (DataRow row in _mainMenuTable.Rows)
            {
                DataRow tempRow = _allMenuTable.Rows.Find(row["ParentMenuID"]);
                if (tempRow != null)
                {
                    row["MenuName"] = tempRow["MenuName"];
                    row["Sort"] = tempRow["Sort"];
                }
            }
            //修改列名
            _mainMenuTable.Columns["ParentMenuID"].ColumnName = "MenuID";

            //排序
            DataView dv = new DataView(_mainMenuTable);
            //去除无效数据
            dv.RowFilter = "MenuID<>'0'";
            dv.Sort = "sort asc";

            rptMain.DataSource = dv;
            rptMain.DataBind();

        }

        /// <summary>
        /// 绑定子菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataTable table;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                table = this._menuTable;
                if (table.Rows.Count > 0)
                {
                    DataView dv = new DataView(table);
                    //筛选出主菜单下相应的子菜单
                    dv.RowFilter = "ParentMenuID ='" + DataBinder.Eval(e.Item.DataItem, "MenuID").ToString() + "'";
                    dv.Sort = " sort asc";

                    Repeater rptChild = (Repeater)e.Item.FindControl("rptChild");
                    rptChild.DataSource = dv;
                    rptChild.DataBind();
                }
            }
        }

        /// <summary>
        /// 获取时段俗称
        /// </summary>
        /// <returns></returns>
        protected string GetTimeSpanNick()
        {
            int hour = DateTime.Now.Hour;
            if (hour < 5)
            {
                return "凌晨";
            }
            else if(hour<12){
                return "上午";
            }
            else if (hour < 18)
            {
                return "下午";
            }
            else { return "晚上"; }
        }

    }
}